import { defineStore } from 'pinia'
import { ReqLogin, reqGetUserinfo } from '@/api/user'
export const useUserStore = defineStore({
	id: 'user',
	state: () => {
		return {
			token: localStorage.getItem('token') || '',
			userInfo:
				JSON.parse(JSON.stringify(localStorage.getItem('userInfo'))) || {}
		}
	},
	actions: {
		async asyncLogin(parmas: any) {
			try {
				let res1 = await ReqLogin(parmas)
				localStorage.setItem('token', res1.token)
				this.token = res1.token
				return true
			} catch (error) {
				return Promise.reject(error)
			}
		},
		async getuserinfo() {
			try {
				let res = await reqGetUserinfo()
				this.userInfo = res
				localStorage.setItem('userInfo', JSON.stringify(res))
				return 'ok'
			} catch (error) {
				return Promise.reject(error)
			}
		},
		reset() {
			localStorage.clear()
			this.token = ''
			this.userInfo = {}
		}
	}
})
